import { crtChannelKs } from "./crtChannelKs";
import crtkc from "./crtkc";


/**疯狂大作战 */
export class crtChannelKs2 extends crtChannelKs {
    channelObj: any = (window as any).ks;
    paraMap: Map<String, string> = new Map([
        ['appId', 'ks662310620508609646'],
        ['vedioAd', '2300016650_01'],
        ['insertAd', ''],
        ['bannerAd', ''],
        ['ctrlId', '677ca009c8946b1700d9564b'], //策略开头id
        ['gid', ''], //gid
    ]);
    /** 游戏名称，用于远程存档,隐私政策等显示用 */
    gameName: string = '疯狂大作战';

    ruanzhu: string = '著作权人：南京桔柚檬网络科技有限公司\n登记号：2022SR0118733';

    /** 分渠道的广告特定动作，actId区分哪个动作,para为执行动作需要的对象
     * 1:恭喜过关页面   0:关闭视频广告,隐藏文字  1:勾选状态  2:隐藏文字,但是视频广告正常显示
     * 2:体力特权/体力补给站/钥匙补给站加文字 观看视频免费领取   0:不勾选  1:勾选
     */

    doAction(actId: number, para: any) {
        let watchAd = ()=>{
            this.showVideo(()=>{

            });
        }
        if(para && para[0] == 'ks2'){
            if(actId == 1){
                let ctrlState = this.getCtrl('p2');
                switch(ctrlState){
                    case 0:
                        para[1].active = false;
                        para[2].active = false;
                        para[3].active = false;
                        break;
                    case 1:
                        para[1].active = true;
                        para[2].active = true;
                        para[3].active = true;
                        break;
                    case 2:
                        para[1].active = true;
                        para[2].active = true;
                        para[3].parent.active = false;
                        break;
                    default:
                        break;
                }
            }else if(actId == 2){
                para[1].active = true;
                let isOpen = this.getCtrl('p3');
                if(isOpen){
                    para[2].active = true;
                    para[3].active = false;
                }else{
                    para[2].active = false;
                    para[3].active = true;
                }
            }
        }
    }



    showVideo(succ): void {
        if(!this.getCtrl('p1')){
            succ && succ();
            return;
        }
        const id = this.getPara('vedioAd');
        console.error('ks showVideo');
        if (!id) {
            return;
        }
        
        cc.game.pause();
        this.rewardedVideoAd = this.channelObj.createRewardedVideoAd({
            adUnitId: id
        });
        if (this.videoFns['onLoad']) {
            this.rewardedVideoAd.offLoad(this.videoFns['onLoad']);
        }
        if (this.videoFns['onError']) {
            this.rewardedVideoAd.offError(this.videoFns['onError']);
        }
        if (this.videoFns['onClose']) {
            this.rewardedVideoAd.offClose(this.videoFns['onClose']);
        }
        this.rewardedVideoAd.offLoad();
        this.rewardedVideoAd.offError();
        this.rewardedVideoAd.offClose();
        const me = this;
        me.videoFns['onLoad'] = () => {
            crtkc.log('激励视频 广告加载成功');
        };
        me.videoFns['onError'] = (err) => {
            crtkc.err(err);
            cc.game.resume();
            // 视频失败切换分享
            this.channelObj.showToast({
                title: '暂无广告',
            });
        }
        me.videoFns['onClose'] = (res) => {
            // 用户点击了【关闭广告】按钮
            // 小于 2.1.0 的基础库版本，res 是一个 undefined
            cc.game.resume();
            if (res && res.isEnded) {
                // 正常播放结束，可以下发游戏奖励
                succ && succ();
            } else {
            }
        };
        me.rewardedVideoAd.onLoad(me.videoFns['onLoad']);
        me.rewardedVideoAd.onError(me.videoFns['onError']);
        me.rewardedVideoAd.onClose(me.videoFns['onClose']);

        me.rewardedVideoAd.show()
            .then(() => {

            }).catch(err => {
                me.rewardedVideoAd.load().then(() => {
                    // 加载成功后需要再显示广告
                    return me.rewardedVideoAd.show().then(() => {
                        
                    }).catch((err) => {
                        cc.game.resume();
                    });
                }).catch((err) => {
                    cc.game.resume();
                });
            });

    }

    doShowInter(isForce?: boolean,  style?: any): void {
        return;
    }


    showInter(isBanner: boolean = false, style?: any) {
        return;
    }



}